package org.tity.test.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.tity.sql.GlobalSettings;
import org.tity.sql.data.AbstractSet;
import org.tity.sql.data.DataNameFormat;
import org.tity.sql.dialect.SQLDialect;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * Hello world!
 *
 */

@Configuration
public class Configs 
{
	
	
	
	private static final boolean useOracle=false;
	
	static void SET () {
		System.out.println(useOracle);
		//设置SQL构建器方言
		if(useOracle)
		{
			GlobalSettings.DEFAULT_SQL_DIALECT=SQLDialect.PLSQL;
		}
		else
		{
			GlobalSettings.DEFAULT_SQL_DIALECT=SQLDialect.MySQL;
		}
 
		//在转格式化的时候字段名大小写规则
		GlobalSettings.DEFAULT_DATA_NAME_FORMAT=DataNameFormat.DB_LOWER_CASE;
		
	}
	
	
	public String getDBHost()
	{
		
//		String ma=Machine.getIdentity();
//		if("3E7F32940AF43757".equals(ma))
//		{
//			return "192.168.1.4";
//		}
//		else if("EB16562F81A2123D".equals(ma))
//		{
//			return "192.168.1.4";
//		}
//		else
//		{
//			return "114.116.30.154";
//		}
		
//		return "47.104.97.13";
		return "127.0.0.1";
	}
	
 
	
	 
	public DruidDataSource getDataSourceOracle() {
		
	
		
		oracle.jdbc.OracleDriver a;
		oracle.jdbc.driver.OracleDriver b;
		
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
 
		dataSource.setUrl("jdbc:oracle:thin:@127.0.0.1:1521:ORCL");
		dataSource.setUsername("STOCK");
		dataSource.setPassword("STOCK");

//		dataSource.setTestOnBorrow(true);
		dataSource.setPoolPreparedStatements(true); // mysql 关闭，Oracle 建议开启
//		dataSource.setTestWhileIdle(true); // 建议配置为true，不影响性能，并且保证安全性。申请连接的时候检测，如果空闲时间大于timeBetweenEvictionRunsMillis，执行validationQuery检测连接是否有效。
		return dataSource;
	}
	
	/**
	 * 设置数据源
	 * */
	@Bean(name="ds")
	public  DruidDataSource getDataSource() {
		
		SET();
		
		if(useOracle)
		{
			return getDataSourceOracle();
		}
		
		// 参考文章 https://www.cnblogs.com/wuyun-blog/p/5679073.html
		
		DruidDataSource dataSource=new DruidDataSource();
//    	dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    	dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
    	dataSource.setUsername("tity");
    	
//    	dataSource.setMinIdle(8);
//    	dataSource.setMaxActive(1024);
    	
    	dataSource.setUrl("jdbc:mysql://"+getDBHost()+":3306/tity_test?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&allowPublicKeyRetrieval=true");
//    	dataSource.setPassword("LeeFJ@aux2018");
    	
    	
//    	dataSource.setUrl("jdbc:mysql://localhost:3306/titydb?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&autoReconnect=true");
    	dataSource.setPassword("tity2018");
//    	dataSource.setTestOnBorrow(true);
//    	dataSource.setValidationQuery("select 1");
//    	dataSource.setPoolPreparedStatements(false);  //mysql  关闭，Oracle 建议开启
//    	dataSource.setTestWhileIdle(true); // 建议配置为true，不影响性能，并且保证安全性。申请连接的时候检测，如果空闲时间大于timeBetweenEvictionRunsMillis，执行validationQuery检测连接是否有效。
		return dataSource;
	}
	
	
	/**
	 * 设置数据源
	 * */
	@Bean(name="sequenceds")
	public  DruidDataSource getSequenceDataSource() {
		
		
		if(useOracle)
		{
			return getDataSourceOracle();
		}
		// 参考文章 https://www.cnblogs.com/wuyun-blog/p/5679073.html
		
		DruidDataSource dataSource=new DruidDataSource();
//    	dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    	dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
    	dataSource.setUsername("tity");
    	
//    	dataSource.setMinIdle(8);
//    	dataSource.setMaxActive(1024);
    	
    	dataSource.setUrl("jdbc:mysql://"+getDBHost()+":3306/titydb_test?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull");
//    	dataSource.setPassword("LeeFJ@aux2018");
    	
//    	dataSource.setUrl("jdbc:mysql://localhost:3306/titydb?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&autoReconnect=true");
    	dataSource.setPassword("tity2018");
//    	dataSource.setTestOnBorrow(false);
//    	dataSource.setPoolPreparedStatements(false);  //mysql  关闭，Oracle 建议开启
//    	dataSource.setTestWhileIdle(true); // 建议配置为true，不影响性能，并且保证安全性。申请连接的时候检测，如果空闲时间大于timeBetweenEvictionRunsMillis，执行validationQuery检测连接是否有效。
		return dataSource;
	}
 
	private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }

	
	@Bean(name=AppDAO.TRANSACTION_MANAGER)
	public  PlatformTransactionManager getTransactionManager(@Qualifier("ds") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}
	/**
	 * 开启跨域访问
	 * */
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }
    
    
    public DruidDataSource getDataSourceDMS() {
		
	
		
		//oracle.jdbc.OracleDriver a;
		//oracle.jdbc.driver.OracleDriver b;
		
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
 
		dataSource.setUrl("jdbc:oracle:thin:@100.100.0.201:1521:orcl");
		dataSource.setUsername("dms_user");
		dataSource.setPassword("dms_user");

//		dataSource.setTestOnBorrow(true);
		dataSource.setPoolPreparedStatements(true); // mysql 关闭，Oracle 建议开启
//		dataSource.setTestWhileIdle(true); // 建议配置为true，不影响性能，并且保证安全性。申请连接的时候检测，如果空闲时间大于timeBetweenEvictionRunsMillis，执行validationQuery检测连接是否有效。
		return dataSource;
	}
	
}
